package com.ita.acmsystem.merchant.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.acmsystem.merchant.entity.Foods;
import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.service.IFoodService;
import com.ita.acmsystem.merchant.service.impl.FoodServiceImpl;

/**
 * Servlet implementation class AjaxAddFoodServlet
 */
public class AjaxAddFoodServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IFoodService foodService = new FoodServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AjaxAddFoodServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		add(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		add(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		String foodId = null, foodName = null, foodPrice = null, foodFoodType = null, foodPic = null, merchantId = null;
		try {
			foodId = UUID.randomUUID().toString();
			foodName = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
			foodPrice = request.getParameter("price");
			foodFoodType = request.getParameter("foodtype");
			foodPic = request.getParameter("foodPic");

			HttpSession session = request.getSession();
			Merchant merchant = (Merchant) session.getAttribute("user");

			// 测试用
			if (merchant == null) {
				merchantId = UUID.randomUUID().toString();
			} else {
				merchantId = merchant.getMerchantId();
			}
			// merchantId = merchant.getMerchantId();

			Foods food = new Foods(foodId, foodName, foodPrice, foodPic,
					foodFoodType, merchantId);

			System.out.println("AjaxAddFoodServlet: " + food);

			PrintWriter writer = response.getWriter();
			ObjectMapper mapper = new ObjectMapper();
			Map<String, String> map = new HashMap<String, String>();

			if (foodService.addFoods(food)) {
				map.put("status", "success");
			} else {
				map.put("status", "error");
				map.put("msg", "添加菜品失败");
			}
			writer.write(mapper.writeValueAsString(map));
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
